//
//  GDThreeButtonView.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/12.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDThreeButtonView.h"
#import "Masonry.h"
#import "CZAdditions.h"

@interface GDThreeButtonView ()

@property(nonatomic, weak)UIView *yellowView;
@property(nonatomic, weak)UIButton *selectedButton;

@end

@implementation GDThreeButtonView{
    NSMutableArray <UIButton *>*_btuArrayM;
}



- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    
    NSArray <NSString *>*btuArray =@[@"商家",@"特价",@"爱好"];
    
    _btuArrayM = [NSMutableArray array];
    
    [btuArray enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
       
        UIButton *button = [UIButton cz_textButton:title fontSize:14 normalColor:[UIColor cz_colorWithHex:0x555555] selectedColor:[UIColor lightGrayColor]];
        
        [button addTarget:self  action:@selector(cilckButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
        if((idx = 0)){
            button.selected = YES;
            self.selectedButton = button;
        }
        
        [_btuArrayM addObject:button];
    }];
    
    
    
    UIView *yellowView = [[UIView alloc]init];
    yellowView.backgroundColor = [UIColor grayColor];
    yellowView.layer.cornerRadius = 3;
    yellowView.layer.masksToBounds = YES;
    
    self.yellowView = yellowView;
    
    [self addSubview:yellowView];
    
    
    //对于数组中的控件的布局
    [_btuArrayM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [_btuArrayM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
    }];
    
    
    [yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.bottom.equalTo(_btuArrayM[0]);
        make.width.mas_equalTo(112);
        make.height.mas_equalTo(5);
    }];
    
}
- (void)setOffset:(CGFloat)offset{
    _offset = offset;
    
    [_yellowView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_btuArrayM[0]).offset(offset);
    }];
    
    [UIView animateWithDuration:1 animations:^{
        
        [self layoutIfNeeded];
        
    }];
    
    //修复小黄条没有一次就弹到指定位置
    NSInteger index = offset/_btuArrayM[0].bounds.size.width + 0.5;
    
    UIButton *button = _btuArrayM[index];
    
    //设置按钮的选中壮态
    self.selectedButton.selected = NO;
    
    button.selected = YES;
    
    self.selectedButton = button;

}
- (void)cilckButton:(UIButton *)sender{
    
    //取消上一个按钮的选中壮态
    self.selectedButton.selected = NO;
    sender.selected = YES;
    self.selectedButton = sender;
    
    //确定点击的倒地是哪一个按钮
    NSInteger index = [_btuArrayM indexOfObject:sender];
    
    //小黄条更新了约束
    [_yellowView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_btuArrayM[0]).offset(index * _btuArrayM[0].bounds.size.width);
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
        [self layoutIfNeeded];
    }];
    
    if([_delegate respondsToSelector:@selector(threeButtonView:withIndex:)]){
        [_delegate threeButtonView:self withIndex:index];
    }
    
    
    
}


@end
